<template>
    <div style="height: 100%;">
        <el-container direction="vertical" style="height: 100%;">
            <el-header class="main-header">
                <div class="header">
                    <div class="logo" @click="toHome">
                        二手交易平台 后台管理
                    </div>
                    <!-- 搜索框 -->
                    <div class="header-main">
                        <!-- <el-input placeholder="请输入内容" v-model="search" class="input-with-select" >
                            <el-button slot="append" icon="el-icon-search"></el-button>
                        </el-input> -->
                    </div>
                    <div class="header-right">
                        <div class="header-user-con">
                           
                            <!-- 用户头像 -->
                            <div class="user-avator">
                                <img src="../../assets/img/user-image.png"                            />
                            </div>
                            <!-- 用户名下拉菜单 -->
                            <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                                <span class="el-dropdown-link">
                                    {{username}}
                                    <i class="el-icon-caret-bottom"></i>
                                </span>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item divided command="system" v-if="isAthor">后台管理</el-dropdown-item>
                                    <el-dropdown-item divided command="center">个人中心</el-dropdown-item>
                                    <el-dropdown-item divided command="changepwd">修改密码</el-dropdown-item>
                                    <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </div>
                    </div>
                    <!-- 修改密码 -->
                    <el-dialog title="修改密码" :visible.sync="changePwdDailog" width="30%">
                        <el-form :model="form" ref="form" :rules="rules">
                            <el-form-item label="原密码" prop="oldpassword">
                            <el-input v-model="form.oldpassword" placeholder="请输入原密码" @blur="checkOldPwd" show-password></el-input>
                            </el-form-item>
                            <el-form-item label="新密码" prop="newpassword">
                            <el-input v-model="form.newpassword" placeholder="请输入新密码" show-password @blur="checkSamePwd"></el-input>
                            </el-form-item>
                            <el-form-item label="确认密码" prop="repassword">
                            <el-input v-model="form.repassword" placeholder="再次输入密码" @blur="checkNewRePwd" show-password></el-input>
                            </el-form-item>
                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="changePwdDailog = false">取 消</el-button>
                            <el-button type="primary" @click="subChangePwd">确 定</el-button>
                        </div>
                    </el-dialog>
                </div>
            </el-header>
            <el-container>
                <el-aside style="width: 15%;">
                    <el-menu @select="handleSelect"
                        default-active="supplySource" style="border-right: none;">
                        <el-menu-item index="1">
                            <i class="el-icon-shopping-cart-1"></i>
                            <span slot="title">商品管理</span>
                        </el-menu-item>
                        <el-menu-item index="2">
                            <i class="el-icon-user"></i>
                            <span slot="title">用户管理</span>
                        </el-menu-item>
                    </el-menu>
                </el-aside>
                <el-main class="main-body">
                    <div class="content">
                        <router-view></router-view>
                    </div>
                </el-main>
            </el-container>
            
        </el-container>
    </div>
</template>

<script>
export default{
    data() {
        return {
            isAthor:false,
            supplySource:'1',
            search:'',
            collapse: false,
            fullscreen: false,
            name: 'linxin',
            message: 2,
            form:[],
            changePwdDailog:false,
            downloadFile:false,
            oldPwdF:false,
            rePwdF:false,
            samePwdF:false,
            rules:{
                newpassword: [
                    {required: true, message:'请输入密码', trigger: 'blur'},
                    {min: 6,max: 32, message: '长度在6到32个字符',trigger:'blur'},
                ],
                oldpassword: [
                    {required: true, message:'请输入原密码', trigger: 'blur'},
                ],
                repassword: [
                    {required: true, message:'请输入确认密码', trigger: 'blur'},
                ],
            },
        };
    },
    computed: {
        username() {
            let username = localStorage.getItem('ms_username');
            this.isAthor = username == 'admin';
            console.log('isathor',this.isAthor)
            return username ? username : this.name;
        }
    },
    methods: {
        goProduct(){
            this.$router.push('/newProduct')
        },
        toHome(){
            this.$router.push('/');
        },
        // 用户名下拉菜单选择事件
        handleCommand(command) {
            if (command == 'loginout') {
                localStorage.removeItem('ms_username');
                this.$router.push('/login');
            }else if (command == 'changepwd') {
                this.changePwd();
            }else if(command == 'center') {
                this.$router.push('/center')
            }else if(command == 'system') {
                this.$router.push('/system/goods')
            }
        },
        //修改密码
        changePwd(){
            this.changePwdDailog = true;
            this.$refs['form'].resetFields();
        },
        //校验原密码
        checkOldPwd(){
            checkPassword({
                password: this.form.oldpassword
            }).then(res=>{
                if(!res.result){
                    this.$message.error('原密码错误');
                    this.oldPwdF = false;
                }else{
                    this.oldPwdF = true;
                }
            })
        },
        checkSamePwd(){
            if(this.form.newpassword === this.form.oldpassword){
                this.$message.error('新密码不能与原密码相同');
                this.samePwdF = false;
            }else {
                this.samePwdF = true;
            }
        },
        //校验新密码
        checkNewRePwd(){
            if(this.form.newpassword !== this.form.repassword){
                this.$message.error('两次密码需要一致');
                this.rePwdF = false;
            }else{
                this.rePwdF = true;
            }
        },
        // 提交修改成功
        subChangePwd(){
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if(this.oldPwdF && this.rePwdF && this.samePwdF){
                        let id = localStorage.getItem('ms_id');
                        userEdit({
                            id: id,
                            password: this.form.newpassword,
                        }).then(res=>{
                            if(res.code == 200){
                                this.$message.success('修改成功,请重新登录');
                                setTimeout(this.$router.push("/login"),3000);
                            }else{
                                this.$message.error('修改失败');
                            }
                        })
                    }   
                }
            })
        },
        //导航栏选择
        handleSelect(index,indexPath){
            console.log(index, indexPath);
            this.supplySource = index;
            switch(index){
                case '1':
                    this.$router.push('/system/goods');
                    break;
                case '2':
                    this.$router.push('/system/users');
                    break;
                    
            }
        }
    },
    mounted() {
        // if (document.body.clientWidth < 1500) {
        //     this.collapseChage();
        // }
    }
}
</script>
<style scoped>
.main-header{
    box-shadow: 0px 3px 2px #dddcdc;
}
.header {
    margin: 0 auto;
    position: relative;
    box-sizing: border-box;
    width: 80%;
    height: 100%;
    font-size: 22px;
    color: black;
    
}
.collapse-btn {
    float: left;
    padding: 0 21px;
    cursor: pointer;
    line-height: 60px;
}
.header .logo {
    float: left;
    /* width: 250px; */
    line-height: 60px;
    font-size: 25px;
    color: #459ff2;
}
.header-main{
    display: inline;
    line-height: 60px;
}
.input-with-select{
    width: 300px;
}


.header .logo img{
    width: 165px;
    margin: 10px 0px;
}
.header-right {
    float: right;
    padding-right: 50px;
}
.header-user-con {
    display: flex;
    height: 60px;
    align-items: center;
}
.btn-fullscreen {
    transform: rotate(45deg);
    margin-right: 5px;
    font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
    position: relative;
    width: 30px;
    height: 30px;
    text-align: center;
    border-radius: 15px;
    cursor: pointer;
}
.btn-bell-badge {
    position: absolute;
    right: 0;
    top: -2px;
    width: 8px;
    height: 8px;
    border-radius: 4px;
    background: #f56c6c;
    color: black;
}
.btn-bell .el-icon-bell {
    color: black;
}
.user-name {
    margin-left: 10px;
}
.user-avator {
    margin-left: 20px;
}
.user-avator img {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
}
.el-dropdown-link {
    color: black;
    cursor: pointer;
}
.el-dropdown-menu__item {
    text-align: center;
}
.main-body{
    background-color: #f5f5f5;
    
}
.main-body .content{
    margin: 0 auto;
    overflow-x: hidden;
}
.content::-webkit-scrollbar {
    display: none;
}
</style>